
package controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;
import model.dao.ProductDao;
import model.dao.TaskDao;
import model.dao.SprintDao;
import model.domain.Product;
import model.domain.Project;
import model.domain.Sprint;
import model.domain.Task;

public class sprintbacklogBean {

    public sprintbacklogBean() {
        this.selectedtask = new Task();
        this.newtask = new Task();
        this.selectedproduct = new Product();
    }

    private Project project;
    private List<Task> taskList;
    private Task selectedtask;
    private Task newtask;
    private Product selectedproduct;

    
    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
	return this.project;
    }

    public Task getNewtask(){
        return this.newtask;
    }

    public void setNewtask(Task object){
        this.newtask = object;
    }
    
    public Task getSelectedtask(){
        return this.selectedtask;
    }

    public void setSelectedtask(Task object){
        this.selectedtask.setTaskID(object.getTaskID());
        this.selectedtask.setStatus(object.getStatus());
        this.selectedtask.setDescription(object.getDescription());
        this.selectedtask.setUser(object.getUser());
        this.selectedtask.setStart(object.getStart());
        this.selectedtask.setEnd(object.getEnd());
        this.selectedtask.setEstimatedEffort(object.getEstimatedEffort());
        this.selectedtask.setComments(object.getComments());
        this.selectedtask.setProductbacklog(object.getProductbacklog());
    }

    public Product getSelectedproduct(){
        return this.selectedproduct;
    }

    public void setSelectedproduct(Product productbacklog){
        this.selectedproduct = productbacklog;
    }

    public List<Task> getSprintBacklog() throws ParseException{
        Sprint currentSprint = this.getCurrentSprint();
        TaskDao home = new TaskDao();
        this.taskList = home.findBySprint(currentSprint);
        return this.taskList;
    }

    public List<Product> getProductBySprint() throws ParseException{
        ProductDao home = new ProductDao();
        return home.findBySprint(this.getCurrentSprint());
    }


    public Sprint getCurrentSprint() throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        Date now = dateFormat.parse(dateFormat.format(cal.getTime()));
        SprintDao home = new SprintDao();
        Sprint currentSprint = home.findByDate(now, this.project);
        return currentSprint;
    }


    public void submitCreate() throws IOException{

        TaskDao home = new TaskDao();

        Task auxTask = new Task();
        auxTask.setDescription(this.newtask.getDescription());
        auxTask.setProductbacklog(this.selectedproduct);
        auxTask.setComments(this.newtask.getComments());
        auxTask.setStatus(this.newtask.getStatus());
        auxTask.setEnd(this.newtask.getEnd());
        auxTask.setEstimatedEffort(this.newtask.getEstimatedEffort());
        auxTask.setStart(this.newtask.getStart());
        auxTask.setUser(this.newtask.getUser());

        home.insert(auxTask);

        this.newtask.setComments("");
        this.newtask.setDescription("");
        this.newtask.setEnd(null);
        this.newtask.setEstimatedEffort(0);
        this.newtask.setStart(null);
        this.newtask.setStatus(null);
        this.newtask.setUser(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("SprintBacklog.xhtml");
    }

    public void submitEdit(){

        TaskDao home = new TaskDao();
        home.insertOrUpdate(this.selectedtask);
    }

    public void submitDelete(){
        TaskDao home = new TaskDao();
        home.delete(this.selectedtask);
    }
}
